package com.ueboot.weishop.api.mq.receiver;

import com.alibaba.fastjson.JSON;
import com.ueboot.core.exception.BusinessException;
import com.ueboot.core.utils.DateUtils;
import com.ueboot.weishop.api.mq.config.QueuesType;
import com.ueboot.weishop.api.mq.receiver.dto.CouponMessageDto;
import com.ueboot.weishop.entity.customer.CustomerCouponEntity;
import com.ueboot.weishop.entity.customer.CustomerInfoEntity;
import com.ueboot.weishop.entity.order.OrderIssueEntity;
import com.ueboot.weishop.service.customer.CustomerCouponService;
import com.ueboot.weishop.service.customer.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * MQ消费-优惠券领券入库处理
 *
 * @author yangkui
 */
@Component
@Slf4j
public class CouponMessageReceiver {

    @Autowired
    private CustomerCouponService customerCouponService;
    @Autowired
    private CustomerService customerService;


    @RabbitHandler
    @RabbitListener(queues = QueuesType.COUPON_SAVE_SYNC, containerFactory = "saveOrderTaskContainerFactory")
    public void couponSave(Object object) {
        try {
            Message m = (Message) object;
            String message = new String(m.getBody(), "UTF-8");
            if (StringUtils.isEmpty(message)) {
                log.info("消息内容为空，不处理！");
                return;
            }
            log.info("保存优惠券订单处理接收到消息:{}", message);


            CouponMessageDto dto = JSON.parseObject(message, CouponMessageDto.class);

            //进行代金券领取动作
            CustomerInfoEntity customerInfoEntity = customerService.findByOpenId(dto.getCustomerOpenId());
            if (customerInfoEntity == null) {
                throw new BusinessException("用户数据异常，请重新访问");
            }


            CustomerCouponEntity couponEntity = new CustomerCouponEntity();
            couponEntity.setBeginTime(DateUtils.parseDate(dto.getBeginDate(), DateUtils.FORMAT_DATE_YYYY_MM_DD));
            couponEntity.setEndTime(DateUtils.parseDate(dto.getEndDate(), DateUtils.FORMAT_DATE_YYYY_MM_DD));
            couponEntity.setCustomer(customerInfoEntity);
            couponEntity.setEnabled(Boolean.TRUE);
            couponEntity.setName(dto.getActName());
            couponEntity.setVin(dto.getVin());
            couponEntity.setSource(dto.getActGroupCode());
            couponEntity.setSourceType(dto.getCustomerType());
            couponEntity.setSeqNo(customerCouponService.getSeqNo());
            couponEntity.setAmount(dto.getAmount());//代金券金额

            OrderIssueEntity issueEntity = new OrderIssueEntity();
            issueEntity.setAmount((dto.getAmount()/100) + "");
            issueEntity.setCampaignCode(dto.getProjectCode());
            issueEntity.setCancel(null);
            issueEntity.setCustomerMobile(customerInfoEntity.getMobile());
            issueEntity.setGoodsName(dto.getActName());
//            issueEntity.setLimitDeduct(dto.getAmount() + "");
            issueEntity.setName(dto.getActName());
            issueEntity.setOpenId(customerInfoEntity.getOpenId());
            issueEntity.setOrderNo(couponEntity.getSeqNo());
            issueEntity.setSeqNo(couponEntity.getSeqNo());
            issueEntity.setTargetDealer(dto.getDealer());
            issueEntity.setVin(dto.getVin());
            issueEntity.setValidBeginDate(dto.getBeginDate());
            issueEntity.setValidEndDate(dto.getEndDate());

            if (dto.getActGroupCode().equals("OIL")) {//合成机油代金券
                issueEntity.setLimitDeduct(dto.getAmount()/100 + "");
                issueEntity.setLowestAmount("260");
            } else {//保养套餐代金券
                issueEntity.setLimitDeduct("0");
                issueEntity.setLowestAmount("10000");
            }
            customerCouponService.save(couponEntity,issueEntity);
            log.info("保存优惠券订单成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
